package com.doublefish.doublefishrobot.ui.home.path.tools;

import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.ui.home.path.bean.BallParamsBean;

/**
 * 作者:dugaofeng on 2025/4/9 15:47
 */
public class S8ParamTranslateUtils {

    public static BallParamsBean s8ParamTranslate(ModeSettingBean.ModeSettingStepBean stepBean) {

        float speed = 0;
        if (stepBean.getSpeedItemBean().isChooseRange()) {
            speed = (stepBean.getSpeedItemBean().getCurrentMaxValue() + stepBean.getSpeedItemBean().getCurrentMinValue()) / 2.0f;
        } else {
            speed = stepBean.getSpeedItemBean().getCurrentValue();
        }

        float elevation = 0;
        if (stepBean.getVerticalItemBean().isChooseRange()) {
            elevation = (stepBean.getVerticalItemBean().getCurrentMaxValue() + stepBean.getVerticalItemBean().getCurrentMinValue()) / 2.0f;
        } else {
            elevation = stepBean.getVerticalItemBean().getCurrentValue();
        }

        float landPoint = 0;
        if (stepBean.getHorizontalItemBean().isChooseRange()) {
            landPoint = (stepBean.getHorizontalItemBean().getCurrentMaxValue() + stepBean.getHorizontalItemBean().getCurrentMinValue()) / 2.0f;
        } else {
            landPoint = stepBean.getHorizontalItemBean().getCurrentValue();
        }

        float sideSpin = 0;
        if (stepBean.getDegreeItemBean().isChooseRange()) {
            sideSpin = (stepBean.getDegreeItemBean().getCurrentMaxValue() + stepBean.getDegreeItemBean().getCurrentMinValue()) / 2.0f;
        } else {
            sideSpin = stepBean.getDegreeItemBean().getCurrentValue();
        }

        float rotation = 0;   //0.不旋  1上旋  2下旋
        if (stepBean.getRotationType() == 0) {
            rotation = 0;
        } else if (stepBean.getRotationType() == 1) {
            if (stepBean.getUpRotationItemBean().isChooseRange()) {
                rotation = (stepBean.getUpRotationItemBean().getCurrentMaxValue() + stepBean.getUpRotationItemBean().getCurrentMinValue()) / 2.0f;
            } else {
                rotation = stepBean.getUpRotationItemBean().getCurrentValue();
            }
        } else if (stepBean.getRotationType() == 2) {
            if (stepBean.getDownRotationItemBean().isChooseRange()) {
                rotation =  - (stepBean.getDownRotationItemBean().getCurrentMaxValue() + stepBean.getDownRotationItemBean().getCurrentMinValue()) / 2.0f;
            } else {
                rotation = -stepBean.getDownRotationItemBean().getCurrentValue();
            }
        }

       return s8ParamTranslate(speed, elevation, landPoint, rotation, sideSpin);
    }



    public static BallParamsBean s8ParamTranslate(float speed, float elevation, float landPoint, float rotation, float sideSpin) {
        BallParamsBean bean = new BallParamsBean();
        bean.setHeight(0.28f);   //默认0.28m

        //速度公式  3.8 + speed * 11.4 / 100
        bean.setSpeed(3.8f + speed * 11.4f / 100);

        //上下旋 正负
        int sign = 1;
        if (rotation < 0) {
            sign = -1;
            rotation = -rotation;  //先把负数 转为正数进行判断
        }
        //上下旋 是非线性的 需要调整
        //0-4
        //5-8   r/2
        //9-12  r/3
        //13     r/4
        if (rotation > 12) {
            rotation = 6 + 4.0f/3 + (rotation - 12) / 4;
        } else if (rotation > 8) {
            rotation = 6 + (rotation - 8) / 3;
        } else if (rotation > 4) {
            rotation = 4 + (rotation - 4) / 2;  //最大值为6
        }
        //重新赋予正负号
        rotation *= sign;

        //上下旋   3度（弧度？） 对应发球机9个档  （maybe 3.14？） 倍数： 3/9=1/3
        bean.setRotation(rotation / 3.0f);

        //仰角8度  对应发球机6个档 倍数： 8/6 = 4/3
        //	仰角补偿7个角度
        //	整体再补偿2个角度
        bean.setElevation((elevation - 25 + 7) * 8.0f / 6 + 2);

        //落点 -15 ~ 15  对应发球机 -25 ~ 25  倍数： 15/25 = 0.6
        bean.setLandPoint((25 - landPoint) * 0.6f);

        //侧旋
        bean.setSideSpin(-sideSpin);

        return bean;
    }
}
